<?php
include "../../inc/log/adsession.php";
?>
<!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
	<?php
	require '../../vendor/autoload.php';
	include "../../inc/data/connAdmin.php";
	//1903010328 引入资源 
	use PhpOffice\PhpSpreadsheet\IOFactory;
	$file = $_FILES["majorfile"]["tmp_name"];
	$spreadSheet = IOFactory::load($file);
	$workSheet = $spreadSheet->getSheet(0);
	$rows = $workSheet->getHighestRow();
	// 1903010328获取动态的学院名 
	$depName = $_POST["dep"];
	for ($i=2; $i <= $rows; $i++) { 
		$sql = "insert into mamx_major 
				(majorname,majorlength,depname) values 
				(?, ?,'$depName')";
		if($stmt=$conn->prepare($sql)){
			//判断专业名称是否为空 
			$majorName = $workSheet->getCell("a$i")->getValue();
			// 查看 
			if ($majorName == ""){
				break;
			}
			// 进行赋值 
			$majorLength = $workSheet->getCell("b$i")->getValue();
			$stmt->bind_param("si",$majorName,$majorLength);
			$stmt->execute();
			if ($stmt->affected_rows > 0) {
				$count += 1;
			}
		}
	}
	if ($count > 0){
		//  录入成功提示 
		echo <<<END
	    <script type="text/javascript">
	    alert("已成功添加 $count 个专业！");
	    history.back();
	    </script>
END;
	}else{
		// 录入失败提示 
		?>
		<script type="text/javascript">
	    alert("添加失败，请重试！");
	    history.back();
	    </script>
	    <?php
	}
	?>
</body>
</html>